var router = require('libs/router').router;
var pages = require('libs/pages');
var ItemMixin = require('mixins/item');
var libs = require('libs/libs');
var _user = SA.get('USER')
var api = libs.api;
var valide = libs.formValide;
var List = require('modules/list/load_list')(true, 'coupons');

//当前页公共变量
var _page = {}

//演示模块
var Show = React.createClass({
    mixins: [ItemMixin],
    componentWillMount: function() {
        if(this.props.data){
            this.setState({
                getCoupons: this.props.data
            })
        }
    },
    willMount: function(){
        console.log(this.state.getCoupons)
        if(this.state.getCoupons.length){
            return(
                <List data={this.state.getCoupons} listClass={'coupons_list'} />
            )
        }else{
            return(
                <div className="uc-message-empty" id="uc-message-empty" style={{display:'block'}}>
                    <div>暂无卡券记录</div>
                </div>
            )
        }
    },
    render: function(){
        var coupons_data = this.willMount();
        return (
            <div className="default_div app_coupons">
                {coupons_data}
            </div>
        );
    }
});

function pullCoupons(){
    var self = this;
    var _coupons = []
    // api.req('/coupons', {login: _user.login, bizId: '1', method: 'get'}, function(data){
    api.req('/coupons', {login: _user.login, method: 'get'}, function(data){
        var coupons = [];               //使用中的数据
        var coupons_nouse = [];         //无法使用的数据
        var couponsStatus;
        if(!data.code){
            data.map(function(item, i){
                if(item.status == 'USE'){
                    couponsStatus = '已生效'
                }
                else if(item.status == 'USE_COMPLETE'){
                    couponsStatus = '已用完'
                }
                else if(item.status == 'INVALID'){
                    couponsStatus = '已失效'
                }
                if(item.createTime){    //只显示年月日
                    couponsTime = item.createTime.substring(0,10)
                }
                if(item.status == 'USE'){    //有效期里的卡劵
                    coupons.push(
                        <div>
                            <div className="coupons_item">
                                <div className="part part-left">
                                    {item.skuName}
                                </div>
                                <div className="part part-right">
                                    {couponsStatus}
                                </div>
                            </div>
                            <div className="part part-left">
                                共<small>{item.totalNum}</small>{item.unit}，剩余<small>{item.remainNum}</small>桶
                            </div>
                            <div className="part part-right">
                                购买日期: {couponsTime}
                            </div>
                        </div>
                    )
                }else{
                    coupons_nouse.push(
                        <div>
                            <div className="coupons_false">
                                <div className="part part-left">
                                    {item.skuName}
                                </div>
                                <div className="part part-right">
                                    {couponsStatus}
                                </div>
                            </div>
                            <div className="part part-right">
                                购买日期: {couponsTime}
                            </div>
                        </div>
                    )
                }
            })
            _coupons = coupons.concat(coupons_nouse);
        }
        _page ={
            data: _coupons
        }
        self.main(self, _page.data)
    })
}

function start(name){
    return pages.new({
        boot: function(self){
        },
        trigger:function(self){
            this.libs.changeTitle('我的卡券');    //更改当前页面标题
            pullCoupons.call(self)
        },
        ready: function(){
        },
        main: function(self, data){
            this.render(
                <Show  data={data} />,
                document.getElementById(name)
            )
        },
        end: function(){
            React.unmountComponentAtNode(document.getElementById(name))
        }
    })
}

module.exports = start;
